﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;
using WorkOrder.Business;
using System.Collections;

namespace WorkOrder
{
    public partial class myChangeover : System.Web.UI.Page
    {
        Business_Table_Changeover B_T_C = new Business_Table_Changeover();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["UserID"] == null || Session["UserID"].ToString() == "")
            {
                Session.Clear();
                ClientScript.RegisterClientScriptBlock(this.GetType(), "windowhref", "<script type='text/javascript'>alert('对不起，您还没有登录系统！');window.parent.location.href='../login.aspx';</script>");
                return;
            }
            if (!Page.IsPostBack)
            {
                #region 加载资料
                ddlProduct.Items.Clear();
                ddlProduct.DataTextField = "product_name";
                ddlProduct.DataValueField = "product_id";
                ddlProduct.DataSource = B_T_C.GetProduct();
                ddlProduct.DataBind();
                ddlProduct.Items.Add(new ListItem("-- 请选择 --", "0"));
                ddlProduct.SelectedIndex = ddlProduct.Items.Count - 1;

                seleTown.Items.Clear();
                seleTown.DataTextField = "town_name";
                seleTown.DataValueField = "town_id";
                seleTown.DataSource = B_T_C.GetTown();
                seleTown.DataBind();
                seleTown.Items.Add(new ListItem("-- 请选择 --", "0"));
                seleTown.SelectedIndex = seleTown.Items.Count - 1;

                ddlFactory.Items.Clear();
                ddlFactory.DataTextField = "fact_name";
                ddlFactory.DataValueField = "fact_id";
                ddlFactory.DataSource = B_T_C.GetFactory();
                ddlFactory.DataBind();
                ddlFactory.Items.Add(new ListItem("-- 请选择 --", "0"));
                ddlFactory.SelectedIndex = ddlFactory.Items.Count - 1;

                ddlSupervision.Items.Clear();
                ddlSupervision.DataTextField = "supe_unit_name";
                ddlSupervision.DataValueField = "supe_unit_id";
                ddlSupervision.DataSource = B_T_C.GetSupervisionUnit();
                ddlSupervision.DataBind();
                ddlSupervision.Items.Add(new ListItem("-- 请选择 --", "0"));
                ddlSupervision.SelectedIndex = ddlSupervision.Items.Count - 1;

                ddlIntegrated.Items.Clear();
                ddlIntegrated.DataTextField = "inte_unit_name";
                ddlIntegrated.DataValueField = "inte_unit_id";
                ddlIntegrated.DataSource = B_T_C.GetIntegratedUnit();
                ddlIntegrated.DataBind();
                ddlIntegrated.Items.Add(new ListItem("-- 请选择 --", "0"));
                ddlIntegrated.SelectedIndex = ddlIntegrated.Items.Count - 1;

                ddlDebuggion.Items.Clear();
                ddlDebuggion.DataTextField = "debug_unit_name";
                ddlDebuggion.DataValueField = "debug_unit_id";
                ddlDebuggion.DataSource = B_T_C.GetDebuggingUnit();
                ddlDebuggion.DataBind();
                ddlDebuggion.Items.Add(new ListItem("-- 请选择 --", "0"));
                ddlDebuggion.SelectedIndex = ddlDebuggion.Items.Count - 1;

                ddlStatus.Items.Clear();
                ddlStatus.DataTextField = "changeover_status";
                ddlStatus.DataValueField = "changeover_status_id";
                ddlStatus.DataSource = B_T_C.GetStatus();
                ddlStatus.DataBind();
                ddlStatus.Items.Add(new ListItem("-- 请选择 --", "0"));
                ddlStatus.SelectedIndex = ddlStatus.Items.Count - 1;

                GridView1.Attributes.Add("SortExpression", "first_cute_time");
                GridView1.Attributes.Add("SortDirection", "DESC");
                txtRow.Text = GridView1.PageSize.ToString();
                #endregion

                BindGridView();
            }
        }

        #region 查询数据
        private void BindGridView()
        {
            string sqlstr = "user_id=" + Session["UserID"].ToString();
            if (!string.IsNullOrEmpty(txtOrderNo.Value.Trim()))
                sqlstr += " and changeover_number='" + Server.HtmlEncode(txtOrderNo.Value.Trim()) + "'";
            if (!string.IsNullOrEmpty(txtBusinessOrderNo.Value.Trim()))
                sqlstr += " and order_number='" + Server.HtmlEncode(txtBusinessOrderNo.Value.Trim()) + "'";
            if (!string.IsNullOrEmpty(txtStation.Text.Trim()))
                sqlstr += " and station_name like '%" + Server.HtmlEncode(txtStation.Text.Trim()) + "%'";
            if (!string.IsNullOrEmpty(txtOrderNo.Value.Trim()))
                sqlstr += " and customer_tell='" + Server.HtmlEncode(txtCustomerTell.Value.Trim()) + "'";
            if (!string.IsNullOrEmpty(txtStation.Text.Trim()))
                sqlstr += " and customer_name like '%" + Server.HtmlEncode(txtCustomer.Value.Trim()) + "%'";
            if (ddlProduct.SelectedValue != "0")
                sqlstr += " and product_id=" + ddlProduct.SelectedValue;
            if (ddlFactory.SelectedValue != "0")
                sqlstr += " and fact_id=" + ddlFactory.SelectedValue;
            if (seleTown.Value != "0")
                sqlstr += " and town_id=" + seleTown.Value;
            else if (seleArea.Value != "0")
            {
                ArrayList arr = B_T_C.GetTownIdByArea(seleArea.Value);
                string tmp = "";
                if (arr != null && arr.Count > 0)
                {
                    for (int i = 0; i < arr.Count; i++)
                        tmp += " or town_id=" + arr[i].ToString();
                }

                sqlstr += " and (" + tmp.Substring(4, tmp.Length - 4) + ")";
            }
            if (ddlSupervision.SelectedValue != "0")
                sqlstr += " and supe_unit_id=" + ddlSupervision.SelectedValue;
            if (ddlIntegrated.SelectedValue != "0")
                sqlstr += " and inte_unit_id=" + ddlIntegrated.SelectedValue;
            if (ddlDebuggion.SelectedValue != "0")
                sqlstr += " and debug_unit_id=" + ddlDebuggion.SelectedValue;
            if (ddlStatus.SelectedValue != "0")
                sqlstr += " and status=" + ddlStatus.SelectedValue;
            if (txtSTART_DATE.Value != "" && txtEND_DATE.Value != "")
                sqlstr += " and first_cute_time between '" + txtSTART_DATE.Value + " 0:0:0' and '" + txtEND_DATE.Value + " 23:59:59'";

            DataTable dt = B_T_C.QueryDatas(sqlstr);

            if (dt != null)
                hidRowCount.Value = dt.Rows.Count.ToString();
            lblRowCount.Text = "共&nbsp;" + hidRowCount.Value + "&nbsp;条记录";

            try { GridView1.PageSize = Convert.ToInt32(txtRow.Text.Trim()); }
            catch { }

            // 获取GridView排序数据列及排序方向
            string sortExpression = this.GridView1.Attributes["SortExpression"];
            string sortDirection = this.GridView1.Attributes["SortDirection"];

            // 根据GridView排序数据列及排序方向设置显示的默认数据视图
            if ((!string.IsNullOrEmpty(sortExpression)) && (!string.IsNullOrEmpty(sortDirection)))
            {
                if (dt != null)
                    dt.DefaultView.Sort = string.Format("{0} {1}", sortExpression, sortDirection);
            }

            // GridView绑定并显示数据
            GridView1.DataSource = dt;
            GridView1.DataBind();
        }
        #endregion

        #region 处理光标行颜色事件
        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowIndex >= 0)
            {
                e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor,this.style.background='#ffe3a0'");
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
                try
                {
                    if (Convert.ToDouble(e.Row.Cells[12].Text) > 0)
                        e.Row.Cells[12].BackColor = System.Drawing.Color.Coral;
                }
                catch { }
                try{
                    if (Convert.ToDouble(e.Row.Cells[17].Text) > 0)
                        e.Row.Cells[17].BackColor = System.Drawing.Color.Coral;
                    }
                catch { }
                try
                {
                    if (Convert.ToDouble(e.Row.Cells[20].Text) > 0)
                        e.Row.Cells[20].BackColor = System.Drawing.Color.Coral;
                    }
                catch { }
                try{
                    if (Convert.ToDouble(e.Row.Cells[14].Text) > 0)
                        e.Row.Cells[14].BackColor = System.Drawing.Color.Coral;
                    }
                catch { }
                try{
                    if (e.Row.Cells[4].Text == "False")
                        e.Row.Cells[4].Text = "否";
                    else
                        e.Row.Cells[4].Text = "是";
                }
                catch { }
            }
        }
        #endregion

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            try
            {
                GridView1.PageIndex = e.NewPageIndex;
                BindGridView();
                TextBox tb = (TextBox)GridView1.BottomPagerRow.FindControl("inPageNum"); tb.Text = (GridView1.PageIndex + 1).ToString();
            }
            catch { }
        }

        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "go")
            {
                try
                {
                    TextBox tb = (TextBox)GridView1.BottomPagerRow.FindControl("inPageNum");
                    int num = Int32.Parse(tb.Text);
                    GridViewPageEventArgs ea = new GridViewPageEventArgs(num - 1);
                    GridView1_PageIndexChanging(null, ea);
                }
                catch
                { }
            }
            if (e.CommandName == "Del")
            {
                if (new Business_Table_Permission().HasDelete(Session["UserID"].ToString(), "Table_Changeover"))
                {
                    if (B_T_C.Delete("changeover_id", e.CommandArgument.ToString(), false))
                        BindGridView();
                    RadAjaxPanel1.ResponseScripts.Add("alert('删除成功！')");
                }
                else
                    RadAjaxPanel1.ResponseScripts.Add("alert('对不起，您没有删除此信息的权限！')");
            }
        }

        protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
        {
            // 从事件参数获取排序数据列
            string sortExpression = e.SortExpression.ToString();

            // 假定为排序方向为“顺序”
            string sortDirection = "ASC";

            // “ASC”与事件参数获取到的排序方向进行比较，进行GridView排序方向参数的修改
            if (sortExpression == this.GridView1.Attributes["SortExpression"])
            {
                //获得下一次的排序状态
                sortDirection = (this.GridView1.Attributes["SortDirection"].ToString() == sortDirection ? "DESC" : "ASC");
            }

            // 重新设定GridView排序数据列及排序方向
            GridView1.Attributes["SortExpression"] = sortExpression;
            GridView1.Attributes["SortDirection"] = sortDirection;

            BindGridView();
        }

        #region 查询按钮事件
        protected void btnSearch_ServerClick(object sender, EventArgs e)
        {
            BindGridView();
        }
        #endregion
    }
}
